//
//  HRRecordButton.m
//  Hear
//
//  Created by mac on 2017/10/20.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRRecordButton.h"
#import "PulsingHaloLayer.h"
#import "EaseChatToolbar.h"
@interface HRRecordButton ()
@property (nonatomic, strong) PulsingHaloLayer *halo;
@property(nonatomic, strong) EaseChatToolbar *easeChatToolbar;
@end
@implementation HRRecordButton

-(instancetype)initWithFrame:(CGRect)frame{ //重写了init方法
    self = [super initWithFrame:frame];
    if (self){
        UIButton *recodButton = [UIButton new];
        [recodButton setImage:[UIImage imageNamed:@"IM_语音讲话"] forState:0];
        [recodButton setAdjustsImageWhenHighlighted:NO];

        [self addSubview:(self.recodButton=recodButton)];
        recodButton.frame = self.bounds;
        
        [recodButton addTarget:self action:@selector(recodButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        NSMutableArray *animationImageArr = [NSMutableArray array];
        for (int i =0; i <3; i++) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"recod_动画%d",i]];
            [animationImageArr addObject:image];
        }
        recodButton.imageView.animationImages = animationImageArr;
        recodButton.imageView.animationDuration = 0.7;
        
    }
    return self;
}

-(PulsingHaloLayer *)halo
{
    if(!_halo){
        
        PulsingHaloLayer *halo = [PulsingHaloLayer layer];
        
        halo.backgroundColor = CustomColor(130, 210, 242, 1).CGColor;
        halo.fromValueForAlpha = 0.8;
        halo.radius = 55;//(self.height-16)/2;
        halo.position = self.recodButton.center;
        
        halo.haloLayerNumber = 4;
        _halo = halo;
    }
    return _halo;
}

-(void)stopOrPause:(NSString *)type
{
    if ([type isEqualToString:@"pause"])
    {
        self.recodButton.selected = NO;
        [self recodButtonClick:self.recodButton];
    }
}

- (void)recodButtonClick:(UIButton *)button{
    button.selected = !button.selected;
    if (button.selected){   //    开始录音
        [self startLarerAnimation];

        [self.recodButton.imageView startAnimating];
    }else{                   //    结束录音
        [self stopLayerAnimation];

        [self.recodButton.imageView stopAnimating];
    }
    if (self.isSelctRecordBlock){
        self.isSelctRecordBlock(button.selected);
    }


}

- (void)stopLayerAnimation{
    if([self.halo superlayer]){
        [self.halo removeFromSuperlayer];
    }
}

- (void)startLarerAnimation{
    if(![self.halo superlayer]){
        [self.recodButton.superview.layer insertSublayer:self.halo below:self.recodButton.layer];
    }
}

- (void)continueRecordImageView
{
    [self startLarerAnimation];
    
    [self.recodButton.imageView startAnimating];
}

@end
